import axios from 'axios';
import { basicAuth } from '@/config/website.json'
import type { OAuth2AccessToken, UserToken } from '@/types/modules/auth'
import { Pagination } from '@/types/global'

const contentPath = '/auth/oauth'

export const oAuthToken = (additionalParameters: any, grantType: string) => {
  return axios.post<OAuth2AccessToken>(`${contentPath}/token`, null, {
    headers: {
      Authorization: `Basic ${basicAuth}`
    },
    params: {
      grant_type: grantType,
      ...additionalParameters
    },
  })
}

export function logout() {
  return axios.post<boolean>(`${contentPath}/logout`)
}

export function revokeToken(accessToken: string) {
  return axios.post<boolean>(`${contentPath}/revoke/token`, null, {
    params: {
      accessToken
    }
  })
}

export function listWithPagination(pageable: any) {
  return axios.get<Pagination<UserToken>>(`${contentPath}/pagination`, {
    params: {
      ...pageable
    }
  })
}
